﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Products;

@model ProductListModel

<div class="row">
	<div class="col-md-12">
		<div class="input-group mb5">
			<input type="text" id="txt_SearchProductStr" class="form-control" placeholder="请输入商品名称、助记名..." onkeydown="if(event.keyCode==13){event.keyCode=0;event.returnValue=false;}">
			<input asp-for="ChannelId" type="hidden" />
			<input asp-for="TerminalId" type="hidden" />
			<input asp-for="BusinessUserId" type="hidden" />
			<input asp-for="WareHouseId" type="hidden" />
			<span class="input-group-btn">
				<button id="btn_query" type="button" class="btn btn-primary" style="border:1px solid #dddddd;"><span class="glyphicon glyphicon-search"></span> 搜索</button>
			</span>
		</div>
	</div>
</div>
<div class="row">
	<div class="col-md-12">

		<div class="tab-block">
			<ul class="nav nav-tabs tabs-bg" id="tabblock">
				<li class="active" data-type="0">
					<a href="#tab10_1" data-toggle="tab" aria-expanded="true"><i class="fa fa-align-justify text-purple"></i> 促销活动</a>
				</li>
				<li data-type="1">
					<a href="#tab10_2" data-toggle="tab"><i class="fa fa-align-justify text-purple"></i> 费用合同</a>
				</li>
			</ul>
			<div class="tab-content">
				<div id="tab10_1" class="tab-pane active">
					<table class="table table-striped table-hover" id="campaigngive_products_datatable"></table>
				</div>
				<div id="tab10_2" class="tab-pane">
					<table class="table table-striped table-hover" id="costcontract_products_datatable"></table>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">

    $(function () {

        //促销活动商品列表
		$("#campaigngive_products_datatable").bootstrapTable({
			url: "/Campaign/AsyncAllGiveProducts",
			method: 'get',
			striped: true,
			cache: false,
			pagination: true,
			queryParams: function (params) {
				var temp =
				{
					key: $("#txt_SearchProductStr").val(),
					terminalId: $("#TerminalId").val(),
					channelId: $("#ChannelId").val(),
					wareHouseId: $("#WareHouseId").val(),
					pageSize: params.limit,
					pageIndex: (params.offset / params.limit)
				};
				return temp;
			},
			sidePagination: "server",
			idField: 'Id',
			pageNumber: 1,
			pageSize: 10,
			search: false,
			strictSearch: true,
			showRefresh: false,
			clickToSelect: false,
			uniqueId: "Id",
			showToggle: false,
			cardView: false,
			detailView: false,
			columns: [
				{ checkbox: true },
				{ field: 'CampaignId', visible: false },
				{ field: 'CampaignName', title: '促销名称' },
				{ field: 'GiveTypeId', visible: false },

				//{ field: 'CampaignBuyProductId', visible: false },
				//{ field: 'Buy_ProductId', visible: false },
				//{ field: 'Buy_ProductName', title: '购买商品' },
				//{ field: 'Buy_UnitConversion', title: '购买单位换算' },
				//{ field: 'Buy_Quantity', title: '购买数量' },
				//{ field: 'Buy_UnitId', visible: false },
				//{ field: 'Buy_UnitName', title: '购买单位' },
				//{ field: 'Buy_Units', visible: false },
				//{ field: 'Buy_Price', visible: false },
				//{ field: 'Buy_CostPrice', visible: false },
				//{ field: 'Buy_Prices', visible: false },
				//{ field: 'Buy_ProductTierPrices', visible: false },

				//{ field: 'Buy_SaleProductTypeId', visible: false },
				//{ field: 'Buy_SaleProductTypeName', visible: false },

				//{ field: 'CampaignGiveProductId', visible: false },
				//{ field: 'Give_ProductId', visible: false },
				//{ field: 'Give_ProductName', title: '赠送商品' },
				//{ field: 'Give_UnitConversion', title: '赠送单位换算' },
				//{ field: 'Give_Quantity', title: '赠送数量' },
				//{ field: 'Give_UnitId', visible: false },
				//{ field: 'Give_UnitName', title: '赠送单位' },
				//{ field: 'Give_Units', visible: false },
				//{ field: 'Give_Price', visible: false },
				//{ field: 'Give_CostPrice', visible: false },
				//{ field: 'Give_Prices', visible: false },
				//{ field: 'Give_ProductTierPrices', visible: false },

				//{ field: 'Give_SaleProductTypeId', visible: false },
				//{ field: 'Give_SaleProductTypeName', visible: false },

				{ field: 'BuyProductMessage', title: '销售' },
				{ field: 'GiveProductMessage', title: '赠送' },

				{ field: 'CampaignBuyProducts', visible: false },
				{ field: 'CampaignGiveProducts', visible: false },

				{ field: 'CampaignLinkNumber', visible: false },
				{ field: 'SaleBuyQuantity', title: '销售商品倍比' },

				{
					field: 'operate', title: '操作', align: 'center', width: '100',
					events: operateEventsPopCampaign,
					formatter: function (value, row, index) {
						return [
							'<button type="button" class="btn btn-sm btn-info rowSelect" data-id="@Model.RowIndex" data-target="@Model.Target"><i class="fa fa-pencil-square-o"></i>&nbsp;选择</button>'
						].join('');
					}
				}
			],
			onClickCell: function (field, value, row, $element) {

				if (field == "Operate") {
					return false;
				}

				var input = $($element).find("input");
				var select = $($element).find("select");
				if (input.length > 0 || select.length > 0)
					return false;

				//当前列所在行的索引
				var index = $element.parent().attr("data-index");
				if (field == "SaleBuyQuantity") {
					var html = '<input type="text" class="form-control input-sm onlyNum" value="' + (typeof (value) == 'undefined' ? "0" : value) + '" id="' + field + "_" + row.Id + '">';

					$($element).html(html);
					var inputObj = $("#" + $(html).attr("id"));

					inputObj.focus();
					inputObj.select();

					inputObj.bind("focusout", function () {
						if ($(this).val() == "") {
							$(this).val(0);
						}
						//数量转整数
						row[field] = parseInt($(this).val());
						$("#campaigngive_products_datatable").bootstrapTable('updateRow', { index: index, row: row });
						$($element).html("" + value + "");
					});

					inputObj.bind("change", function () {
						if (!isNaN($(this).val())) {
							if ($(this).val() == "") {
								$(this).val(0);
							}
						}
						else {
							$(this).val(0);
							$(this).showToastr("只能输入数字!");
						}
					});
				}

			},

            onPageChange: function (number, size) {
                m_pagerow = (number - 1) * size;
            },
            onLoadSuccess: function (data) {
                //console.log('load');
            }
        });

        //费用合同商品列表
        $("#costcontract_products_datatable").bootstrapTable({
            url: "/CostContract/AsyncAllGiveProducts",
            method: 'get',
            striped: true,
            cache: false,
            pagination: true,
            queryParams: function (params) {
                var temp =
                {
                    key: $("#txt_SearchProductStr").val(),
					customerId: $("#TerminalId").val(),
					businessUserId: $("#BusinessUserId").val(),
					wareHouseId: $("#WareHouseId").val(),
                    pageSize: params.limit,
                    pageIndex: (params.offset / params.limit)
                };
                return temp;
            },
            sidePagination: "server",
            idField: 'Id',
            pageNumber: 1,
            pageSize: 10,
            search: false,
            strictSearch: true,
            showRefresh: false,
            clickToSelect: false,
            uniqueId: "Id",
            showToggle: false,
            cardView: false,
            detailView: false,
            columns: [
				{ checkbox: true },
				{ field: 'CostContractBillId', visible: false },
				{ field: 'Month', visible: false },
				{ field: 'CType', visible: false },
				{ field: 'ContractTypeName', title: '合同类型' },
				{ field: 'MonthName', title: '月份' },
				{ field: 'ProductId', visible: false },
                { field: 'ProductName', title: '商品名称/现金' },
				{ field: 'AvailableQuantityOrAmount', title: '剩余数量/金额' },
				{ field: 'UnitId', visible: false },
				{ field: 'UnitName', title: '单位' },
				{ field: 'Units', visible: false },
				{ field: 'SaleProductTypeId', visible: false },
				{ field: 'SaleProductTypeName', visible: false },
				{ field: 'GiveTypeId', visible: false },

                {
                    field: 'operate', title: '操作', align: 'center', width: '100',
					events: operateEventsPopCostContract,
                    formatter: function (value, row, index) {
                        return [
                            '<button type="button" class="btn btn-sm btn-info rowSelect" data-id="@Model.RowIndex" data-target="@Model.Target"><i class="fa fa-pencil-square-o"></i>&nbsp;选择</button>'
                        ].join('');
                    }
                }
            ],
            onPageChange: function (number, size) {
                m_pagerow = (number - 1) * size;
            },
            onLoadSuccess: function (data) {
                //console.log('load');
            }
        });

        //查询
        $("#btn_query").on('click', function () {
			var keys = $("#txt_SearchProductStr").val();
			var curTab = null;
			$("#tabblock").find("li").each(function (a,b) {

				if ($(b).hasClass('active')) {
					curTab = $(this).attr("data-type");
					return false;
				}
			});

			if (curTab != null && curTab == 0) {
				$("#campaigngive_products_datatable").bootstrapTable('refresh');
			}
			else if (curTab != null && curTab == 1) {
				$("#costcontract_products_datatable").bootstrapTable('refresh');
			}
        });

        //保存选择
        $("#productSelect_btnSave").click(function () {
            var curTab = null;
            $("#tabblock").find("li").each(function () {
                if ($(this).hasClass('active')) {
                    curTab = $(this).attr("data-type");
                    return false;
                }
            });

            if (curTab != null && curTab == 0) {
                var arrselections = $.map($("#campaigngive_products_datatable").bootstrapTable('getSelections'), function (row) { return row.Id });
                if (arrselections.length <= 0) { $(this).showToastr('请选择有效数据'); return; }
            }
            else if (curTab != null && curTab == 1) {
                var arrselections = $.map($("#costcontract_products_datatable").bootstrapTable('getSelections'), function (row) { return row.Id });
                if (arrselections.length <= 0) { $(this).showToastr('请选择有效数据'); return; }
            }
        });
    });
</script>